const ModelUser = require('../../models/user/user_admin'),
    uuid = require('uuid'),
    FileTool = require('../../utils/filetool'),
    ComTool = require('../../utils/commonTools');

const fileSavePath = "/images/users_avatar_url";

// 删除用户
exports.delUser = (req, res, next) => {
    const { admin_id, avatar, avatar_url } = req.body;
    ModelUser.delUser({
        admin_id
    }, (value) => {
        if (avatar_url && avatar)
            FileTool.removeFile('./public' + avatar_url, avatar);
        res.render('success', {
            data: JSON.stringify({
                message: "删除成功",
                status: 200,
                affectedRows: value.affectedRows
            })
        })
    }, (error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "删除失败",
                status: 200,
                error
            })
        })
    });
}

// 编辑用户
exports.updateUser = async (req, res, next) => {
    const { user, file } = req.body;

    let newData = file ? await FileTool.saveImage(file, './public' + fileSavePath) : null;

    if (newData === false) {
        return res.render('fail', {
            data: JSON.stringify({
                message: "图片保存失败",
                status: 200,
            })
        })
    }
    else {
        if (user.avatar_url && user.avatar)
            FileTool.removeFile('./public' + user.avatar_url, user.avatar);
    }

    let Data = {
        ...user,
        avatar_url: newData ? fileSavePath + '/' + newData.saveFloder + '/' : null,
        avatar: newData ? newData.name : null,
        update_time: ComTool.getCurrentTime()
    }

    const { admin_id } = Data;
    if (admin_id) {
        delete Data.admin_id;
        ModelUser.updateUser(Data, {
            admin_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

// 是否禁用
exports.isLockUser = (req, res, next) => {
    const { admin_id, is_locked } = req.body;
    if (admin_id && is_locked) {
        ModelUser.updateUser({
            is_locked
        }, {
            admin_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

// 查找所有用户
exports.list = async (req, res, next) => {
    ModelUser.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        });
}

// 查找用户
exports.findUser = async (req, res, next) => {
    let User = await ModelUser.findUser(req.body);
    User.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到用户",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到用户",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    User.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}

// 登录
exports.loginUser = async (req, res, next) => {
    const { username, password } = req.body;
    if (username && password) {
        let User = await ModelUser.findUser({
            username, password
        });
        User.success((value) => {
           
            if (value.length) {
                if(value[0].is_locked == -1){
                    return res.render('fail', {
                        data: JSON.stringify({
                            message: "该用户已被冷冻",
                            value: value,
                            length: value.length
                        })
                    })
                }
                res.render('success', {
                    data: JSON.stringify({
                        message: "查找到用户",
                        value: value,
                        status: 200,
                        length: value.length
                    })
                })
            } else {
                res.render('fail', {
                    data: JSON.stringify({
                        message: "没有找到用户,查看用户名或密码是否错误",
                        value: value,
                        length: value.length
                    })
                })
            }
        });

        User.fail((error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    error
                })
            })
        })
    }
}

// 添加用户
exports.addUser = async (req, res, next) => {
    const { username } = req.body;
    let User = await ModelUser.findUser({ username }),
        bool = User.success((value) => {
            if (value.length) return true;
        });

    if (bool) {
        return res.render('fail', {
            data: JSON.stringify({
                status: 200,
                affectedRows: 0,
                message: "用户已经存在"
            })
        })
    }

    ModelUser.addUser({
        ...req.body,
        admin_id: uuid.v1()
    },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}